package com.whut.whw.topic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.util.EmptyUtils;

import java.util.List;

/**
 * <pre>
 *  desc: 专题适配器
 *  Created by 忘尘无憾 on 2017/06/07.
 *  version: 1.0
 * </pre>
 */
public class TopicStyleAdapter extends BaseAdapter {

    private Context context;
    private List<TopicModel> list;
    private LayoutInflater layoutInflater;

    /**
     * TopicStyleAdapter的适配器
     *
     * @param context 上下文
     * @param list    list集合
     */
    public TopicStyleAdapter(Context context, List<TopicModel> list) {
        this.context = context;
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        //判断list是否为空，不为空则获取其长度，否则为0
        return EmptyUtils.isNotEmpty(this.list) ? this.list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (EmptyUtils.isEmpty(convertView)) {
            convertView = this.layoutInflater.inflate(R.layout.fragment_topic_item_grid, null);
        }

        //从fragment_topic_item_grid中获取组件
        TextView topic_title_tv = (TextView) convertView.findViewById(R.id.topic_title_tv);
        TextView topic_content_tv = (TextView) convertView.findViewById(R.id.topic_content_tv);
        ImageView topic_img_iv = (ImageView) convertView.findViewById(R.id.topic_img_iv);

        //从list对象中为组件赋值
        TopicModel topicModel = this.list.get(position);
        topic_title_tv.setText(topicModel.getTopic_title().toString().trim());
        topic_content_tv.setText(topicModel.getTopic_content().toString().trim());
        //采用Glide的方式加载图片
        Glide
                .with(this.context)
                .load(topicModel.getTopic_img())
                .into(topic_img_iv);
        return convertView;
    }
}
